package com.gs.commentvl.users.service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

import com.gs.commentvl.dao.PostDao;
import com.gs.commentvl.model.Post;
import com.gs.commentvl.web.parameter.Result;

@Service
@EnableScheduling
public class PostServiceImpl extends GenericService<Post> implements PostService {

	@Autowired
	private PostDao postDao;
	@Autowired
	private SimpMessagingTemplate template;

	private static Set<Post> dataPost = new LinkedHashSet<Post>();

	@Override
	public Post addNewContent(Post post) {
		//timestamp
		long currentTime = System.currentTimeMillis();
		post.setPostedTime(currentTime);

		//add expire time
		 Calendar cal = Calendar.getInstance();
		 cal.add(Calendar.MINUTE, 60);
		 post.setExpiredTime(cal.getTime().getTime());

		post.setPostId(postDao.getPostKey());

		postDao.addNew(post);
		//sendNewPostNotifications(post);
		synchronized(dataPost){
			dataPost.add(post);
		}

		return post;
	}

	@Override
	public Set<Post> getAllPost() {

		Set<Post> posts = postDao.getAll();
		if (posts != null && posts.size() > 0){
			return posts;
		}
		return new LinkedHashSet<Post>();
	}

	@Override
	public Set<Post> getPostLimit (long offset, long count) {
		Set<Post> posts = postDao.getPostLimit(offset, count);
		if (posts != null && posts.size() > 0){
			return posts;
		}

		return new LinkedHashSet<Post>();
	}

//	private void sendNewPostNotifications(Post post) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
//
//		Set<Post> data = new LinkedHashSet<Post>();
//		data.add(post);
//
//		Result res = new Result();
//		res.setType("new");
//		res.setData(data);
//
//		//send broadcast
//		template.convertAndSend("/topic/posts", res, map);
//	}

	@Scheduled(fixedDelay=20000) //20 seconds
	public void sendNewPostNotifications() {

		synchronized(dataPost){
			if (dataPost.size() > 0) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);

				Result res = new Result();
				res.setType("new");
				res.setData(dataPost);

				//send broadcast
				template.convertAndSend("/topic/posts", res, map);
				dataPost.clear();
			}
		}

//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
//
//			Set<Post> posts = getAllPost();
//
//			//template.convertAndSendToUser(posts, "/queue/position-updates", result.position, map);
//			template.convertAndSend("/topic/posts", posts, map);
//			//template.convertAndSendToUser(user, destination, payload);



//			for (TradeResult result : this.tradeResults) {
//				if (System.currentTimeMillis() >= (result.timestamp + 1500)) {
//					logger.debug("Sending position update: " + result.position);
//					this.messagingTemplate.convertAndSendToUser(result.user, "/queue/position-updates", result.position, map);
//					this.tradeResults.remove(result);
//				}
//			}
		}

	@Override
	public long getPostNum() {
		return postDao.getPostNum();
	}
}
